package servlet.request.exercises;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-16
 * Time:21:24
 */
@WebServlet(name = "ComputerServlet", urlPatterns = "/computerServlet")
public class ComputerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userAgent = req.getHeader("User-Agent");
        String regex = "\\((.*?)\\)";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(userAgent);
        if (matcher.find()) {
            String group = matcher.group(1);
            String[] strings = group.split(";");
            System.out.println("操作系统: " + strings[0]);
            System.out.println("操作系统位数: " + strings[1].trim());
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
